<?php
class Notify_m extends CI_Model{
	
	public $user_name = '';
	function __construct(){//重载父类的数据库连接方法
		parent::__construct();
		$this->load->helper('string');
		$this->user_name = $this->session->userdata('user_name');
		$this->user_id = $this->session->userdata('user_id');
		$this->dept_name = $this->session->userdata('dept_name');
		$this->CURD = date("Y-m-d");
	}
	/*--接收列表---------------------------------------------*/
	function getinboxlistdata($data){
		$this->db->from('notify');
		if(array_key_exists("title",$data))
			$this->db->like('title', $data['title']); 
		if(array_key_exists("sendname",$data))
			$this->db->where_in('from_name',explode(",",reduce_multiples($data['sendname'],',',true)));
		if(array_key_exists("eids",$data))
			$this->db->where('begin_date >=', $data['eids']);
		if(array_key_exists("eide",$data)){
			$eide = date('Y-m-d',strtotime($this->input->post('eide')." + 1 day"));
			$this->db->where('end_date <=', $eide);
		}
				
		$this->db->where('begin_date <=', date("Y-m-d"));
		$this->db->where('end_date >=', date("Y-m-d"));
		$this->db->like('to_id', $this->user_id.',');
		
		//在order、group或limit前查询总数
        $db = clone($this->db);
        $total = $this->db->count_all_results();
		$this->db = $db;		
		$this->db->order_by($data['sort'], $data['order']); 
		$start=($data['page']-1)*$data['rows'];
		$this->db->limit($data['rows'],$start);
		$query = $this->db->get();
		$data = $query->result_array();
		return array('data'=>$data, 'total'=>$total);
	}
	
/*--已发布列表---------------------------------------------*/
	function getsentboxlistdata($data){
		$this->db->from('notify');
		if(array_key_exists("title",$data))
			$this->db->like('title', $data['title']); 
		if(array_key_exists("sendname",$data))
			$this->db->where_in('from_name',explode(",",reduce_multiples($data['sendname'],',',true)));
		if(array_key_exists("eids",$data))
			$this->db->where('begin_date >=', $data['eids']);
		if(array_key_exists("eide",$data)){
			$eide = date('Y-m-d',strtotime($this->input->post('eide')." + 1 day"));
			$this->db->where('end_date <=', $eide);
		}
		$this->db->where('from_id', $this->user_id);
		
		//在order、group或limit前查询总数
        $db = clone($this->db);
        $total = $this->db->count_all_results();
		
		$this->db = $db;
		$this->db->order_by($data['sort'], $data['order']); 
		$start=($data['page']-1)*$data['rows'];
		$this->db->limit($data['rows'],$start);
		$query = $this->db->get();
		$data = $query->result_array();
		return array('data'=>$data, 'total'=>$total);
	}

/*
	function sentbox_all($searchParams = ''){	
		if($searchParams){
			if(@$searchParams['title'])
				$this->db->like('title', $searchParams['title']); 
			if(@$searchParams['sender'])
				$this->db->like('to_name',$searchParams['sender']);
			if(@$searchParams['eids'])
				$this->db->where('begin_date >=', $searchParams['eids']);
			if(@$searchParams['eide'])
				$this->db->where('end_date <=', $searchParams['eide']);
		}
		$this->db->where('from_id', $this->user_id);
		$query=$this->db->get('notify');
		return $query->num_rows();
	}
	function sentbox_limit($start,$end,$sort,$order,$searchParams = ''){		
		if($searchParams){
			if(@$searchParams['title'])
				$this->db->like('title', $searchParams['title']); 
			if(@$searchParams['sender'])
				$this->db->like('to_name',$searchParams['sender']);
			if(@$searchParams['eids'])
				$this->db->where('begin_date >=', $searchParams['eids']);
			if(@$searchParams['eide'])
				$this->db->where('end_date <=', $searchParams['eide']);
		}
		$this->db->where('from_id', $this->user_id);
		$this->db->order_by($sort, $order); 
		$this->db->limit($end,$start);
		$query=$this->db->get('notify');
		//echo $this->db->last_query();//输出随后一次查询的语
		return $query->result();
	}
	*/
	//通知公告详细内容
	function contentbox($id){
		$this->db->where('notify_id', $id);
		$query=$this->db->get('notify');
		$row = $query->result();
		$pos = strpos($row[0]->reader,$this->user_name);
		if($pos === false){
			$arr = array('reader' => $row[0]->reader.$this->user_name.",");
			$this->db->where('notify_id', $row[0]->notify_id);
			if($this->db->update('notify', $arr)){
				$this->db->where('notify_id', $id);
				$query=$this->db->get('notify');
				return $query->result();
			}else{
				return false;
			}
		}else{
			return $row;	
		}
	}
	
	//添加新通告数据到数据库
	function set_add_post($data){
		$uidarr = explode(",",reduce_multiples($data['p_sj_id'],',',true));
		$user_arr = explode(",",reduce_multiples($data['p_sj_name'],',',true));
		
		//是否重要
		$p_top = (@$data['p_top'] == true)?1:0;
		
		//是否短信提醒
		$p_sms = (@$data['p_sms'] == true)?1:0;

		//开始事务处理
		$this->db->trans_begin();
			if($data['p_notify_id'] == ''){
				
				$end_date = ($data['p_end_date'] == '')?'2050-00-00':$data['p_end_date'];
				$array = array(//邮件主体数据
					'notify_id' => '', 
					'from_id' => $this->user_id, 
					'from_name' => $this->user_name, 
					'to_id' => $data['p_sj_id'],
					'to_name' => $data['p_sj_name'], 
					'reader' => '', 
					'title'=> $data['p_title'],
					'content'=> $data['p_content'],
					'attachment_id'=> $data['p_att_id'],
					'attachment_name'=> $data['p_att_name'],
					'size'=> $data['p_att_size'],
					'top'=> $p_top,
					'sms_remind'=> $p_sms,
					'type_id'=> $data['p_type'],	
					'dept_name'=> $this->dept_name,		
					'url_add'=> '',	
					'sendertime'=> date("Y-m-d",time()),
					'begin_date'=> $data['p_begin_date'],
					'end_date'=> $end_date,
					'notify_color'=> @$data['p_notify_color']
				);
				$this->db->set($array);
				$this->db->insert('notify'); //插入邮件主体
				$notify_id = $this->db->insert_id();
	
				if($p_sms == 1){
					//邮件短信提醒数据主体插入到数据库			
					$smsarr = array(
						'bodyid' => '' ,
						'sender' => $this->user_name,
						'smstype' => '3',
						'alerttype' => '0',
						'content' => '通知公告！ 主题：'.$data['p_title'],
						'sendtime' => date("Y-m-d H:i:s",time()),
						'remindurl' => 'notify/inbox/'.$notify_id
					 ); 
					$this->db->insert('sms_body', $smsarr); //插入短信提醒主体数据
					$bodyid2 = $this->db->insert_id();
					
					for($i = 0; $i<count($uidarr);$i++){
						$arr[1][] = array(//短信收信人数据
							'smsid' => '',
							'to_id' => $uidarr[$i],
							'to_name' => $user_arr[$i],
							'sms_read' => '0',
							'readtime' => '',
							'sms_delete' => '0',
							'bodyid' => $bodyid2
						);
						$arr[2][] = array(//用于检索的数据表数据
							'id' => '',
							'u_id' => $uidarr[$i],
							'u_name' => $user_arr[$i],
							'add_date' => time(),
							'm_id' => '3_'.$bodyid2
						);
					}
					$this->db->insert_batch('sms',$arr[1]); //插入接收邮件数组
					$this->db->insert_batch('sms_check',$arr[2]); //插入接收邮件数组
				}
			}else{
				$array = array(//邮件主体数据
					'to_id' => $data['p_sj_id'],
					'to_name' => $data['p_sj_name'], 
					'reader' => '', 
					'title'=> $data['p_title'],
					'content'=> $data['p_content'],
					'attachment_id'=> $data['p_att_id'],
					'attachment_name'=> $data['p_att_name'],
					'size'=> $data['p_att_size'],
					'top'=> $p_top,
					'sms_remind'=> $p_sms,
					'type_id'=> '1',
					'url_add'=> '',	
					'begin_date'=> $data['p_begin_date'],
					'end_date'=> $data['p_end_date'],
					'notify_color'=> ''
				);	
				$this->db->where('notify_id', $data['p_notify_id']);
				$this->db->update('notify', $array); 
			}

		
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}
	
	//设置为已读
	function set_read($id){
		$this->db->trans_begin();
			foreach($id['getid'] as $id){
				$this->db->where('notify_id', $id);
				$query=$this->db->get('notify');
				$row = $query->result();
				$pos = strpos($row[0]->reader,$this->user_name);
				if($pos === false){
					$arr = array('reader' => $row[0]->reader.$this->user_name.",");
					$this->db->where('notify_id', $id);
					$this->db->update('notify', $arr);
				}
			}
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}
	

	
	/*删除通知公告方法*/
	function notify_del($data){//收件箱删除邮件方法
		$this->db->trans_begin();//开始事务处理
		foreach($data['getid'] as $id){
			$this->db->where('notify_id', $id);
			$this->db->delete('notify');
		}
		if($this->db->trans_status() === FALSE){
			$this->db->trans_rollback();//事件回滚	
			return false;		
		}else{
			$this->db->trans_commit();//提交成功
			return true;
		}//事务结束
	}
}
?>